const { createEmotionPlugin } = require('emotion-ts-plugin')
const createTsImportTransformer = require('ts-import-plugin')

const { cypressTransformer } = require('./cypress')

function isEnv(env) {
  return process.env.NODE_ENV === env
}

module.exports = () => {
  const before = []

  // 在线上环境也暴露 data-cypress 属性，便于做集成测试
  if (!isEnv('test')) {
    before.push(cypressTransformer)
  }

  const emotionTransformer = createEmotionPlugin({
    sourcemap: !isEnv('test'),
    autoInject: true,
    jsxImportSource: '@emotion/react',
  })
  before.push(emotionTransformer)

  if (isEnv('production') && typeof process.env.ssr === 'undefined') {
    const tsImportTransformer = createTsImportTransformer([
      {
        style: false,
        libraryName: 'lodash',
        libraryDirectory: null,
        camel2DashComponentName: false,
      },
      {
        style: false,
        libraryName: '@fluentui/react',
        libraryDirectory: 'lib',
        camel2DashComponentName: false,
        transformToDefaultImport: false,
      },
    ])

    before.push(tsImportTransformer)
  }

  return { before }
}
